package com.ruoyi.popAndHou.controller;

import java.util.List;

import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.popAndHou.domain.entityVo;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.popAndHou.domain.PopAndHou;
import com.ruoyi.popAndHou.service.IPopAndHouService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人房关联Controller
 *
 * @author wang
 * @date 2022-04-20
 */
@RestController
@RequestMapping("/popAndHou/popAndHou")
public class PopAndHouController extends BaseController
{
    @Autowired
    private IPopAndHouService popAndHouService;
    @Autowired
    private IXlkjPopulationService populationService;
    @Autowired
    private IXlkjHouseService houseService;

    /**
     * 获取后端下拉列表数据
     * 废物接口，查询数据太庞大，作废
     */
    @GetMapping("/selectTree")
    public AjaxResult selectTree(){
        List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(null);
        for(XlkjPopulation population:xlkjPopulations) {
            population.setName(population.getName() + "——" + population.getAddress());
        }
        List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(null);
//        for(XlkjHouse house:xlkjHouses) {
//            house.setName(house.getName() + "——" + house.getAddress());
//        }
        return AjaxResult.success(new entityVo(xlkjPopulations,xlkjHouses));
    }

    /**
     * 查询人房关联列表
     */
    @PreAuthorize("@ss.hasPermi('popAndHou:popAndHou:list')")
    @GetMapping("/list")
    public TableDataInfo list(PopAndHou popAndHou)
    {
        startPage();
        List<PopAndHou> list = popAndHouService.selectPopAndHouList(popAndHou);
//        for(PopAndHou p:list){
//            XlkjPopulation xlkjPopulation = populationService.selectXlkjPopulationById(p.getPopulationId());
//            if(xlkjPopulation!=null){
//                p.setName(xlkjPopulation.getName());
//                p.setCode(xlkjPopulation.getCertificatesNum());
//            }
//
//
//            XlkjHouse house = houseService.selectXlkjHouseById(p.getHouseId());
//
//            if(house!=null){
//                p.setAddress(house.getAddress());
//            }
//
//        }
        return getDataTable(list);
    }

    /**
     * 导出人房关联列表
     */
    @PreAuthorize("@ss.hasPermi('popAndHou:popAndHou:export')")
    @Log(title = "人房关联", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PopAndHou popAndHou)
    {
        List<PopAndHou> list = popAndHouService.selectPopAndHouList(popAndHou);
        ExcelUtil<PopAndHou> util = new ExcelUtil<PopAndHou>(PopAndHou.class);
        return util.exportExcel(list, "人房关联数据");
    }

    /**
     * 获取人房关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('popAndHou:popAndHou:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(popAndHouService.selectPopAndHouById(id));
    }

    /**
     * 新增人房关联
     */
    @PreAuthorize("@ss.hasPermi('popAndHou:popAndHou:add')")
    @Log(title = "人房关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PopAndHou popAndHou)
    {
        return toAjax(popAndHouService.insertPopAndHou(popAndHou));
    }

    /**
     * 修改人房关联
     */
    @PreAuthorize("@ss.hasPermi('popAndHou:popAndHou:edit')")
    @Log(title = "人房关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PopAndHou popAndHou)
    {
        return toAjax(popAndHouService.updatePopAndHou(popAndHou));
    }

    /**
     * 删除人房关联
     */
    @PreAuthorize("@ss.hasPermi('popAndHou:popAndHou:remove')")
    @Log(title = "人房关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(popAndHouService.deletePopAndHouByIds(ids));
    }
}
